<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    body{background-color: #FFFFFF}
    .change_page{background-color: #FFFFFF}
    td{padding: 0!important;}
    .xuan{text-align: center;width: 45px!important;}
    .xuanname{padding-left: 10px}
    #list_list .buttonnone{position: absolute;right: 0;padding: 0 6px!important;top: -1px;color: #707070;background-color: #fff;border: 1px solid #e6e6e6;}
    #list_list .buttonlv{position: absolute;right: 0;padding: 0 6px!important;top: -1px;background-color: #248BFE}
</style>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con">
            <span id="flowsetname">选择分部项</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>选择分部项</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45"><input type="checkbox" class="xuanhang" lay-filter="xuanhang" lay-skin="primary"></th>
                            <th>分部行名称</th>
                            <th>完成比率(留空默认为1)</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>

    var CurrentFennameArr = parent.CurrentFennameArr;
    var parent_qingid = getQueryVariable('qingid');
    var param_enginid = getQueryVariable('enginid');


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        var existObj;
        var existStr =  $("#list_list .fenname_"+parent_qingid , parent.document).text() || '';
        if(existStr){
            try{
                existObj = JSON.parse(existStr);
            }catch (e) {
                existObj = '';
            }
        }else{
            existObj = [];
        }

        var str = '';
        for (var key in CurrentFennameArr) {
            var isCun = is_in_arr(CurrentFennameArr[key], existObj);
            var nums = parseInt(key)+1;
            if(isCun.fenname == CurrentFennameArr[key]){
                str += '<tr>';
                str += '<td class="xuan"><input type="checkbox" class="xuanhang" checked lay-skin="primary"></td>';
                str += '<td><input type="text" readonly class="layui-input fenname" value="'+CurrentFennameArr[key]+'"></td>';
                str += '<td><input type="text" class="layui-input rate"  value="'+isCun.rate+'"></td>';
                str += '</tr>';
            }else{
                str += '<tr>';
                str += '<td class="xuan"><input type="checkbox" class="xuanhang" lay-skin="primary"></td>';
                str += '<td><input type="text" readonly class="layui-input fenname" value="'+CurrentFennameArr[key]+'"></td>';
                str += '<td><input type="text" class="layui-input rate" ></td>';
                str += '</tr>';
            }
        }
        $("#list_list").html(str);
        render_from();

        form.on('checkbox(xuanhang)',function (data) {
            if($(data.elem).prop("checked")){
                $('#list_list .xuanhang').prop("checked", true);
            }else{
                $('#list_list .xuanhang').prop("checked", false);
            }
            render_from();
        });



        form.on('submit(add)', function(data){
            var dataArr = [];
            $('#list_list tr').each(function (i) {
                var checked = $(this).find('.xuanhang').prop("checked");
                var fenname = $(this).find('.fenname').val() || '';
                var rate = $(this).find('.rate').val() || '';
                if(checked){
                    dataArr.push({
                        fenname: fenname,
                        rate: rate,
                    });
                }
            });


            var datastr = JSON.stringify(dataArr);
            if(dataArr.length > 0){
                $("#list_list .fennamebut_"+parent_qingid , parent.document).removeClass('buttonnone').addClass('buttonlv');
            }else{
                $("#list_list .fennamebut_"+parent_qingid , parent.document).removeClass('buttonlv').addClass('buttonnone');
            }

            $("#list_list .fenname_"+parent_qingid , parent.document).text(datastr);

            layer_close();
            return false;
        });

    });


    $('#list_list').on('focus','.rate',function () {
        var val = $(this).val();
        if(val == ''){
            $(this).val('=');
        }
    });
    $('#list_list').on('blur','.rate',function () {
        var val = $(this).val();
        if(val == '='){
            $(this).val('');
        }
    });


    function is_in_arr(name, arr) {
        var data = {
            fenname: '',
            rate: '',
        };
        if(!arr || arr.length < 1){
            return data;
        }
        for (var key in arr) {
            if(arr[key].fenname == name){
                data.fenname = arr[key].fenname;
                data.rate = arr[key].rate;
            }
        }

        return data;
    }
</script>
</body>
</html>
